<?php
/**
 * 商品订单控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\OrderLog;
use app\admin\logic\OrderLogic;
use app\common\model\Order as CommonOrder;
use app\common\model\Pay;
use think\Db;

class OrderController extends Controller
{

    //列表
    public function index(Request $request)
    {
        if ($this->request->isAjax()) {
            $get = $this->request->get();
            $list=OrderLogic::lists($get);
            return json([
                'code' =>1,
                'msg'  =>'',
                'data' =>$list
            ]);
        }
        $this->assign('order_status', CommonOrder::getOrderStatus(true));
        $this->assign('order_type', CommonOrder::getOrderType(true));
        $this->assign('pay_way', Pay::getPayWay(true));
        return $this->fetch();
    }

    /**
     * Desc: 订单详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $this->assign('detail', $detail);
        $this->assign('logs', OrderLog::getOrderLog($id));
        return $this->fetch();
    }

    /**
     * Desc: 发货
     */
    public function delivery()
    {
        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $this->assign('detail', $detail);
        $this->assign('express', OrderLogic::express());
        return $this->fetch();
    }

    /**
     * Desc: 发货操作
     */
    public function deliveryHandle()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\Order.delivery');
            if (true !== $check) {
                return error($check);
            }
            OrderLogic::deliveryHandle($post,$this->user->id);
            return success('发货成功');
        }
    }

     /**
     * Desc: 物流信息
     */
    public function express()
    {
        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $detail['shipping'] = OrderLogic::shippingInfo($detail['id']);
        $this->assign('detail', $detail);
        return $this->fetch();
    }

    /**
     * @notes 修改快递单号
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function change_delivery()
    {
        $get = $this->request->get();
        if($this->request->isAjax() && $this->request->isPost())
        {
            $post = $this->request->post();
            $result = OrderLogic::changeDelivery($post);
            if ($result) {
                return success('操作成功');
            }
        }

        $detail = OrderLogic::getDelivery($get);
        $this->assign('detail', $detail);
        $this->assign('express', OrderLogic::express());
        return $this->fetch();
    }

    /**
     * Desc: 确认收货
     */
    public function confirm()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\Order.confirm');
            if (true !== $check) {
                return error($check);
            }
            OrderLogic::confirm($post['order_id'],$this->user->id);
            return success('确认成功');
        }
    }
}
